# -*- coding: iso-8859-15 -*-
from sintax import sintax
#
# 	ANALISADOR LEXICO
#
#  1   Separa blocos

class token:
	def __init__(self, tkstr):
		self.tipo = "nenhumtipo"
		self.tok = tkstr
		self.col = -1
		self.lin = -1
	
	def __str__(self):
		return "<token " + self.tipo +  " -> '" + self.tok + "' >"
	def gettoken(self):
		return self.tok
	def gettipo(self):
		return self.tipo

class scanner:
	def __init__(self, nameoffile):
		self.line=""
		self.sintax = sintax()
		self.expr = [' ', '..', ',','(',')', '[', ']', '++', '+','-', '*', '/', '=', ':',';', ]
		self.ebloco = (["\"","\\",],  ["\"", "\r\n"],  ["(*", "*)"])
		self.buffer_line = []
		self.prox_char = None # proximo caracter
		self.end_token = False # indica o final dos tokens no arquivo
		self.end_file = False # indica o final do arquivo 
		
		self.nmfile = nameoffile
		self.f = open(self.nmfile, "r")
	
	def bloco(self):
		for i in self.ebloco[0]:
			pass
	
	def readlineinfile(self):
		linha = self.f.readline()
		# É o final do arquivo
		if len(linha) <> 0:
			# troca todas as novas linhas por espaço em branco
			return linha
		else:
			self.end_file=True
			return None
			
	def getnexttoken(self):
		# Se o tem tokens no buffer retornar eles
		if len(self.buffer_line)>0:
			return self.buffer_line.pop()
		else:
			self.fragtoken(self.readlineinfile())
			#print "BUFFERLINE", str(self.buffer_line)
			self.buffer_line.reverse()
			if len(self.buffer_line)>0:
				t = self.buffer_line.pop()
				#print t
				return t
				
			
	def fragtoken(self, line):
		if line <> None:
			#print "LINE", line
			line = line.replace("\n"," ").replace("\r","").replace("\t"," ").replace(str(chr(13))+str(chr(10))," ")
			# verifica o inicio de um bloco
			for a in self.ebloco:
				if line.find(a[0])<>-1:
					bufferbloco = line[line.find(a[0]):]
					line = line[0:line.find(a[0])]
			#dicionario com informacoes do local e tamanho
			findexpr = {}
			index = 0 
			while index<len(line):
				if line[index:index+2] in self.expr:
					findexpr[index] = 2
					index+=2
				elif line[index] in self.expr:
					findexpr[index] = 1
					index+=1
				else:
					index+=1
			keys = findexpr.keys()
			keys.sort()
			print keys
			ant = None
			print "EXPR", findexpr
			print "LINE", line
			print "KEYS", keys
			for i in keys:
				if ant<>None:
					indexant = ant+findexpr[ant]
				else:
					indexant = 0
				pala = line[indexant:i]
				ex = line[i:i+findexpr[i]]
				ant = i
				pala = pala.replace(" ", "")
				if pala<>'':
					t = token(pala)
					self.sintax.settypeintoken(t)
					self.buffer_line.append(t)
				ex = ex.replace(" ", "")
				if ex<>'':
					t = token(ex)
					self.sintax.settypeintoken(t)
					print t
					self.buffer_line.append(t)
		
	def testeallfile(self):
		list = []
		while not self.end_file:
			list.append(str(self.getnexttoken()))
		for i in list:
			print i
		
if __name__=="__main__":
	p = scanner("teste.pas")
	p.testeallfile()
